package com.szpx.controller;

import com.szpx.entity.Goods;
import com.szpx.entity.TBUser;
import com.szpx.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/main/goods/cart/")
    public ResponseEntity<String> addToCart(HttpSession session, @RequestParam("gid") int productId, @RequestParam("cnt") int quantity) {

        TBUser tbUser = (TBUser) session.getAttribute("user");


        boolean added = cartService.addToCart(tbUser.getUid(), productId, quantity);
        if (added) {
            return ResponseEntity.ok("商品成功加入购物车");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("添加到购物车失败");
        }
    }
}
